package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.system.entity.DapSystemSchema;
import cn.com.bluemoon.daps.system.entity.DapSystemTag;
import cn.com.bluemoon.daps.system.entity.DapSystemTagGroup;
import cn.com.bluemoon.daps.system.mapper.DapSystemSchemaMapper;
import cn.com.bluemoon.daps.system.mapper.DapSystemTagGroupMapper;
import cn.com.bluemoon.daps.system.mapper.DapSystemTagMapper;
import cn.com.bluemoon.daps.system.service.DapSystemSchemaService;
import cn.com.bluemoon.daps.system.service.DapSystemTagGroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DapSystemTagGroupServiceImpl  extends ServiceImpl<DapSystemTagGroupMapper, DapSystemTagGroup>
        implements DapSystemTagGroupService {

    @Autowired
    DapSystemTagMapper dapSystemTagMapper;

    @Override
    public Boolean checkGroupInfo(String name, String id) {

        List<DapSystemTagGroup> dapSystemTagGroups = this.getBaseMapper()
                .selectList(new LambdaQueryWrapper<DapSystemTagGroup>()
                        .eq(DapSystemTagGroup::getTagGroupName, name));
        Optional<DapSystemTagGroup> first = dapSystemTagGroups.stream().findFirst();
        return id == null ? !first.isPresent() : first.isPresent() ? first.get().getId().equals(id) : true;
    }

    @Override
    public String checkGroupIsUse(String id) {

        List<DapSystemTag> dapSystemTags = dapSystemTagMapper.selectList(new LambdaQueryWrapper<DapSystemTag>().eq(DapSystemTag::getTagGroupId, id)
                .eq(DapSystemTag::getBmStatus, BmStatus.ON.getCode()));
        return dapSystemTags.isEmpty() ? null : dapSystemTags.stream().map(DapSystemTag::getName).collect(Collectors.joining(","));
    }
}
